package com.qyer.search.utils;

import com.qyer.search.Constants;
import com.qyer.search.filter.AbstractFilter;
import com.qyer.search.filter.AbstractIDFilter;
import com.qyer.search.model.SearchParameter;
import com.qyer.search.model.SearchParameterEntity;
import com.qyer.search.model.SearchParameterOrigin;
import com.qyer.search.model.aggregation.AggregationProduct;
import com.qyer.search.model.aggregation.AggregationRegular;
import com.qyer.search.model.lastminute.LightLessZSKU;
import com.qyer.search.model.lastminute.LightZSKU;
import com.qyer.search.model.lastminute.SKU;
import com.qyer.search.model.lastminute.SimpleZSKU;
import com.qyer.search.model.lastminute.VSKU;
import com.qyer.search.model.lastminute.ZSKU;
import com.qyer.search.model.mutable.Place;
import com.qyer.search.model.result.SearchEntity;
import com.qyer.search.model.result.Type1SearchEntity;
import com.qyer.search.model.result.Type1SearchPackage;
import com.qyer.search.model.result.SearchPackage;
import com.qyer.search.model.result.VSTGSearchSKUs;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import java.util.*;

/**
 * User: Z J Wu Date: 2016/01/05 Time: 18:54 Package: com.qyer.search.utils
 */
public class ModelUtils {

  /**
   * 目前, 1016, 1017, 1018, 1020这些小类型都算做2667大自由行里的内容.
   *
   * @param type 一级类目
   * @return 合并的一级类目
   */
  public static int mergeType1(int type) {
    switch (type) {
      case 1016:
        return 2667;
      case 1017:
        return 2667;
      case 1018:
        return 2667;
      case 1020:
        return 2667;
      default:
        return type;
    }
  }

  public static List<Type1SearchPackage> packageByType1(SearchParameter sp,
                                                        List<String> resultStringList,
                                                        boolean simple) {
    if (CollectionUtils.isEmpty(resultStringList)) {
      return Collections.emptyList();
    }
    Map<Integer, Type1SearchPackage> packageMap = new HashMap<>();
    SKU sku;
    int type1;
    Type1SearchPackage searchPackage;
    Map<String, Object> skuMap;
    for (String resultString : resultStringList) {
      try {
        skuMap = JsonOperator.getInstance().fromJson(resultString, Map.class);
      } catch (Exception e) {
        continue;
      }
      sku = SKU.build(sp, skuMap, simple);
      if (sku == null) {
        continue;
      }
      type1 = sku.getType1();
      searchPackage = packageMap.get(type1);
      if (searchPackage == null) {
        searchPackage = new Type1SearchPackage(type1);
        packageMap.put(type1, searchPackage);
      }
      searchPackage.addSku(sku);
    }
    List<Type1SearchPackage> list = new ArrayList<>(packageMap.size());
    for (Map.Entry<Integer, Type1SearchPackage> entry : packageMap.entrySet()) {
      list.add(entry.getValue());
    }
    return list;
  }

  public static List<Type1SearchPackage> packageByType1(SearchParameterOrigin sp,
                                                        List<String> resultStringList,
                                                        boolean simple) {
    if (CollectionUtils.isEmpty(resultStringList)) {
      return Collections.emptyList();
    }
    Map<Integer, Type1SearchPackage> packageMap = new HashMap<>();
    SKU sku;
    int type1;
    Type1SearchPackage searchPackage;
    Map<String, Object> skuMap;
    for (String resultString : resultStringList) {
      try {
        skuMap = JsonOperator.getInstance().fromJson(resultString, Map.class);
      } catch (Exception e) {
        continue;
      }
      sku = SKU.build(sp, skuMap, simple);
      if (sku == null) {
        continue;
      }
      type1 = sku.getType1();
      searchPackage = packageMap.get(type1);
      if (searchPackage == null) {
        searchPackage = new Type1SearchPackage(type1);
        packageMap.put(type1, searchPackage);
      }
      searchPackage.addSku(sku);
    }
    List<Type1SearchPackage> list = new ArrayList<>(packageMap.size());
    for (Map.Entry<Integer, Type1SearchPackage> entry : packageMap.entrySet()) {
      list.add(entry.getValue());
    }
    return list;
  }

  public static List<Type1SearchPackage> packageByType1(SearchParameterOrigin sp,
                                                        List<String> resultStringList,
                                                        boolean simple, List<AbstractFilter> filters) {
    if (CollectionUtils.isEmpty(resultStringList)) {
      return Collections.emptyList();
    }
    Map<Integer, Type1SearchPackage> packageMap = new HashMap<>();
    SKU sku;
    int type1;
    Type1SearchPackage searchPackage;
    Map<String, Object> skuMap;
    for (String resultString : resultStringList) {
      try {
        skuMap = JsonOperator.getInstance().fromJson(resultString, Map.class);
      } catch (Exception e) {
        continue;
      }
      sku = SKU.build(sp, skuMap, simple, filters);
      if (sku == null) {
        continue;
      }
      type1 = sku.getType1();
      searchPackage = packageMap.get(type1);
      if (searchPackage == null) {
        searchPackage = new Type1SearchPackage(type1);
        packageMap.put(type1, searchPackage);
      }
      searchPackage.addSku(sku);
    }
    List<Type1SearchPackage> list = new ArrayList<>(packageMap.size());
    for (Map.Entry<Integer, Type1SearchPackage> entry : packageMap.entrySet()) {
      list.add(entry.getValue());
    }
    return list;
  }

  public static List<Type1SearchPackage> packageByType1(SearchParameterOrigin sp,
                                                        List<String> resultStringList,
                                                        boolean simple, boolean basic) {
    long t1 = System.currentTimeMillis();
    if (CollectionUtils.isEmpty(resultStringList)) {
      return Collections.emptyList();
    }
    Map<Integer, Type1SearchPackage> packageMap = new HashMap<>();
    SKU sku = null;
    int type1;
    Type1SearchPackage searchPackage;
    Map<String, Object> skuMap;
    for (String resultString : resultStringList) {
      try {
        skuMap = JsonOperator.getInstance().fromJson(resultString, Map.class);
      } catch (Exception e) {
        continue;
      }
      if(basic){
        sku = SKU.buildBasicSKU(sp, skuMap);
      }else {
        sku = SKU.build(sp, skuMap, simple);
      }
      if (sku == null) {
        continue;
      }
      type1 = sku.getType1();
      searchPackage = packageMap.get(type1);
      if (searchPackage == null) {
        searchPackage = new Type1SearchPackage(type1);
        packageMap.put(type1, searchPackage);
      }
      searchPackage.addSku(sku);
    }
    List<Type1SearchPackage> list = new ArrayList<>(packageMap.size());
    for (Map.Entry<Integer, Type1SearchPackage> entry : packageMap.entrySet()) {
      list.add(entry.getValue());
    }
    long t2 = System.currentTimeMillis();
    //System.out.println("内部反序列化时间:"+(t2 - t1));
    return list;
  }

  public static List<Type1SearchPackage> packageByType1(SearchParameterOrigin sp,
                                                        List<String> resultStringList,
                                                        boolean simple, boolean basic,
                                                        Map<Integer,Map<Integer,Integer>> placeSearchCensus,
                                                        Map<Integer,Map<Integer,Integer>> typeSearchCensus) {
    if (CollectionUtils.isEmpty(resultStringList)) {
      return Collections.emptyList();
    }
    Map<Integer, Type1SearchPackage> packageMap = new HashMap<>();
    SKU sku = null;
    int type1;
    Type1SearchPackage searchPackage;
    Map<String, Object> skuMap;
    for (String resultString : resultStringList) {
      try {
        skuMap = JsonOperator.getInstance().fromJson(resultString, Map.class);
      } catch (Exception e) {
        continue;
      }
      if(basic){
        sku = SKU.buildBasicSKU(sp, skuMap, placeSearchCensus, typeSearchCensus);
      }else {
        sku = SKU.build(sp, skuMap, simple, placeSearchCensus, typeSearchCensus);
      }
      if (sku == null) {
        continue;
      }
      type1 = sku.getType1();
      searchPackage = packageMap.get(type1);
      if (searchPackage == null) {
        searchPackage = new Type1SearchPackage(type1);
        packageMap.put(type1, searchPackage);
      }
      searchPackage.addSku(sku);
    }
    List<Type1SearchPackage> list = new ArrayList<>(packageMap.size());
    for (Map.Entry<Integer, Type1SearchPackage> entry : packageMap.entrySet()) {
      list.add(entry.getValue());
    }
    return list;
  }

  public static List<Type1SearchPackage> packageByType1(SearchParameterOrigin sp,
                                                        List<String> resultStringList,
                                                        boolean simple, boolean basic,
                                                        Map<Integer,Map<Integer,Integer>> placeSearchCensus,
                                                        Map<Integer,Map<Integer,Integer>> typeSearchCensus, int start, int end) {
    if (CollectionUtils.isEmpty(resultStringList)) {
      return Collections.emptyList();
    }
    Map<Integer, Type1SearchPackage> packageMap = new HashMap<>();
    SKU sku = null;
    int type1;
    Type1SearchPackage searchPackage;
    Map<String, Object> skuMap;
    for (int i = start; i < end; i++){
    //for (String resultString : resultStringList) {
      String resultString = resultStringList.get(i);
      try {
        skuMap = JsonOperator.getInstance().fromJson(resultString, Map.class);
      } catch (Exception e) {
        continue;
      }
      if(basic){
        sku = SKU.buildBasicSKU(sp, skuMap, placeSearchCensus, typeSearchCensus);
      }else {
        sku = SKU.build(sp, skuMap, simple, placeSearchCensus, typeSearchCensus);
      }
      if (sku == null) {
        continue;
      }
      type1 = sku.getType1();
      searchPackage = packageMap.get(type1);
      if (searchPackage == null) {
        searchPackage = new Type1SearchPackage(type1);
        packageMap.put(type1, searchPackage);
      }
      searchPackage.addSku(sku);
    }
    List<Type1SearchPackage> list = new ArrayList<>(packageMap.size());
    for (Map.Entry<Integer, Type1SearchPackage> entry : packageMap.entrySet()) {
      list.add(entry.getValue());
    }
    return list;
  }

  public static SearchPackage packageAll(SearchParameterOrigin sp,
                                                        List<String> resultStringList,
                                                        boolean simple, boolean basic,
                                         Map<Integer,Map<Integer,Integer>> placeSearchCensus,
                                         Map<Integer,Map<Integer,Integer>> typeSearchCensus) {
    if (CollectionUtils.isEmpty(resultStringList)) {
      return null;
    }

    SearchPackage searchPackage = new SearchPackage();
    SKU sku = null;
    int type1;
    Map<String, Object> skuMap;
    for (String resultString : resultStringList) {
      try {
        skuMap = JsonOperator.getInstance().fromJson(resultString, Map.class);
      } catch (Exception e) {
        continue;
      }
      if(basic){
        sku = SKU.buildBasicSKU(sp, skuMap, placeSearchCensus, typeSearchCensus);
      }else {
        sku = SKU.build(sp, skuMap, simple, placeSearchCensus, typeSearchCensus);
      }
      if (sku == null) {
        continue;
      }
      searchPackage.addSku(sku);
    }
    return searchPackage;
  }

  public static SearchPackage packageAll(SearchParameterOrigin sp,
                                         List<String> resultStringList,
                                         boolean simple, boolean basic,
                                         Map<Integer,Map<Integer,Integer>> placeSearchCensus,
                                         Map<Integer,Map<Integer,Integer>> typeSearchCensus,
                                         int start, int end) {
    if (CollectionUtils.isEmpty(resultStringList)) {
      return null;
    }

    SearchPackage searchPackage = new SearchPackage();
    SKU sku = null;
    int type1;
    Map<String, Object> skuMap;
    for (int i = start; i < end; i++){
    //for (String resultString : resultStringList) {
      String resultString = resultStringList.get(i);
      try {
        skuMap = JsonOperator.getInstance().fromJson(resultString, Map.class);
      } catch (Exception e) {
        continue;
      }
      if(basic){
        sku = SKU.buildBasicSKU(sp, skuMap, placeSearchCensus, typeSearchCensus);
      }else {
        sku = SKU.build(sp, skuMap, simple, placeSearchCensus, typeSearchCensus);
      }
      if (sku == null) {
        continue;
      }
      searchPackage.addSku(sku);
    }
    return searchPackage;
  }

  public static SearchPackage packageAll(SearchParameterOrigin sp,
                                         List<String> resultStringList,
                                         boolean simple, boolean basic) {
    if (CollectionUtils.isEmpty(resultStringList)) {
      return null;
    }

    SearchPackage searchPackage = new SearchPackage();
    SKU sku = null;
    int type1;
    Map<String, Object> skuMap;
    for (String resultString : resultStringList) {
      try {
        skuMap = JsonOperator.getInstance().fromJson(resultString, Map.class);
      } catch (Exception e) {
        continue;
      }
      if(basic){
        sku = SKU.buildBasicSKU(sp, skuMap);
      }else {
        sku = SKU.build(sp, skuMap, simple);
      }
      if (sku == null) {
        continue;
      }
      searchPackage.addSku(sku);
    }
    return searchPackage;
  }

  public static List<Type1SearchPackage> packageByType1(SearchParameterOrigin sp,
                                                        List<String> resultStringList,
                                                        boolean simple, boolean basic, List<AbstractFilter> filters) {
    if (CollectionUtils.isEmpty(resultStringList)) {
      return Collections.emptyList();
    }
    Map<Integer, Type1SearchPackage> packageMap = new HashMap<>();
    SKU sku = null;
    int type1;
    Type1SearchPackage searchPackage;
    Map<String, Object> skuMap;
    for (String resultString : resultStringList) {
      try {
        skuMap = JsonOperator.getInstance().fromJson(resultString, Map.class);
      } catch (Exception e) {
        continue;
      }
      if(basic){
        sku = SKU.buildBasicSKU(sp, skuMap, filters);
      }else {
        sku = SKU.build(sp, skuMap, simple, filters);
      }
      if (sku == null) {
        continue;
      }
      type1 = sku.getType1();
      searchPackage = packageMap.get(type1);
      if (searchPackage == null) {
        searchPackage = new Type1SearchPackage(type1);
        packageMap.put(type1, searchPackage);
      }
      searchPackage.addSku(sku);
    }
    List<Type1SearchPackage> list = new ArrayList<>(packageMap.size());
    for (Map.Entry<Integer, Type1SearchPackage> entry : packageMap.entrySet()) {
      list.add(entry.getValue());
    }
    return list;
  }

  public static List<Type1SearchPackage> packageByType1(SearchParameterOrigin sp,
                                                        List<String> resultStringList,
                                                        boolean simple, boolean basic, List<AbstractFilter> filters,
                                                        Map<Integer,Map<Integer,Integer>> placeSearchCensus,
                                                        Map<Integer,Map<Integer,Integer>> typeSearchCensus) {
    if (CollectionUtils.isEmpty(resultStringList)) {
      return Collections.emptyList();
    }
    Map<Integer, Type1SearchPackage> packageMap = new HashMap<>();
    SKU sku = null;
    int type1;
    Type1SearchPackage searchPackage;
    Map<String, Object> skuMap;
    for (String resultString : resultStringList) {
      try {
        skuMap = JsonOperator.getInstance().fromJson(resultString, Map.class);
      } catch (Exception e) {
        continue;
      }
      if(basic){
        sku = SKU.buildBasicSKU(sp, skuMap, filters, placeSearchCensus, typeSearchCensus);
      }else {
        sku = SKU.build(sp, skuMap, simple, filters, placeSearchCensus, typeSearchCensus);
      }
      if (sku == null) {
        continue;
      }
      type1 = sku.getType1();
      searchPackage = packageMap.get(type1);
      if (searchPackage == null) {
        searchPackage = new Type1SearchPackage(type1);
        packageMap.put(type1, searchPackage);
      }
      searchPackage.addSku(sku);
    }
    List<Type1SearchPackage> list = new ArrayList<>(packageMap.size());
    for (Map.Entry<Integer, Type1SearchPackage> entry : packageMap.entrySet()) {
      list.add(entry.getValue());
    }
    return list;
  }

  public static List<Type1SearchPackage> packageByType1(SearchParameterOrigin sp,
                                                        List<String> resultStringList,
                                                        boolean simple, boolean basic, List<AbstractFilter> filters,
                                                        Map<Integer,Map<Integer,Integer>> placeSearchCensus,
                                                        Map<Integer,Map<Integer,Integer>> typeSearchCensus,
                                                        int start, int end) {
    if (CollectionUtils.isEmpty(resultStringList)) {
      return Collections.emptyList();
    }
    Map<Integer, Type1SearchPackage> packageMap = new HashMap<>();
    SKU sku = null;
    int type1;
    Type1SearchPackage searchPackage;
    Map<String, Object> skuMap;
    for (int i = start; i < end; i++){
    //for (String resultString : resultStringList) {
      String resultString = resultStringList.get(i);
      try {
        skuMap = JsonOperator.getInstance().fromJson(resultString, Map.class);
      } catch (Exception e) {
        continue;
      }
      if(basic){
        sku = SKU.buildBasicSKU(sp, skuMap, filters, placeSearchCensus, typeSearchCensus);
      }else {
        sku = SKU.build(sp, skuMap, simple, filters, placeSearchCensus, typeSearchCensus);
      }
      if (sku == null) {
        continue;
      }
      type1 = sku.getType1();
      searchPackage = packageMap.get(type1);
      if (searchPackage == null) {
        searchPackage = new Type1SearchPackage(type1);
        packageMap.put(type1, searchPackage);
      }
      searchPackage.addSku(sku);
    }
    List<Type1SearchPackage> list = new ArrayList<>(packageMap.size());
    for (Map.Entry<Integer, Type1SearchPackage> entry : packageMap.entrySet()) {
      list.add(entry.getValue());
    }
    return list;
  }

  public static SearchPackage packageAll(SearchParameterOrigin sp,
                                         List<String> resultStringList,
                                         boolean simple, boolean basic,List<AbstractFilter> filters,
                                         Map<Integer,Map<Integer,Integer>> placeSearchCensus,
                                         Map<Integer,Map<Integer,Integer>> typeSearchCensus) {
    if (CollectionUtils.isEmpty(resultStringList)) {
      return null;
    }

    SearchPackage searchPackage = new SearchPackage();
    SKU sku = null;
    int type1;
    Map<String, Object> skuMap;
    for (String resultString : resultStringList) {
      try {
        skuMap = JsonOperator.getInstance().fromJson(resultString, Map.class);
      } catch (Exception e) {
        continue;
      }
      if(basic){
        sku = SKU.buildBasicSKU(sp, skuMap, filters, placeSearchCensus, typeSearchCensus);
      }else {
        sku = SKU.build(sp, skuMap, simple, filters, placeSearchCensus, typeSearchCensus);
      }
      if (sku == null) {
        continue;
      }
      searchPackage.addSku(sku);
    }
    return searchPackage;
  }

  public static SearchPackage packageAll(SearchParameterOrigin sp,
                                         List<String> resultStringList,
                                         boolean simple, boolean basic,List<AbstractFilter> filters,
                                         Map<Integer,Map<Integer,Integer>> placeSearchCensus,
                                         Map<Integer,Map<Integer,Integer>> typeSearchCensus,
                                         int start, int end) {
    if (CollectionUtils.isEmpty(resultStringList)) {
      return null;
    }

    SearchPackage searchPackage = new SearchPackage();
    SKU sku = null;
    int type1;
    Map<String, Object> skuMap;
    for (int i =start; i < end; i++){
    //for (String resultString : resultStringList) {
      String resultString = resultStringList.get(i);
      try {
        skuMap = JsonOperator.getInstance().fromJson(resultString, Map.class);
      } catch (Exception e) {
        continue;
      }
      if(basic){
        sku = SKU.buildBasicSKU(sp, skuMap, filters, placeSearchCensus, typeSearchCensus);
      }else {
        sku = SKU.build(sp, skuMap, simple, filters, placeSearchCensus, typeSearchCensus);
      }
      if (sku == null) {
        continue;
      }
      searchPackage.addSku(sku);
    }
    return searchPackage;
  }

  public static SearchPackage packageAll(SearchParameterOrigin sp,
                                         List<String> resultStringList,
                                         boolean simple, boolean basic,List<AbstractFilter> filters) {
    if (CollectionUtils.isEmpty(resultStringList)) {
      return null;
    }

    SearchPackage searchPackage = new SearchPackage();
    SKU sku = null;
    int type1;
    Map<String, Object> skuMap;
    for (String resultString : resultStringList) {
      try {
        skuMap = JsonOperator.getInstance().fromJson(resultString, Map.class);
      } catch (Exception e) {
        continue;
      }
      if(basic){
        sku = SKU.buildBasicSKU(sp, skuMap, filters);
      }else {
        sku = SKU.build(sp, skuMap, simple, filters);
      }
      if (sku == null) {
        continue;
      }
      searchPackage.addSku(sku);
    }
    return searchPackage;
  }


  //new search
  public static List<Type1SearchEntity> packageByType1(SearchParameterEntity sp, List<String> resultStringList,
                                                        List<AbstractFilter> filters, AggregationRegular regular,
                                                        AggregationProduct productCensus, int result_mode) {
    if (CollectionUtils.isEmpty(resultStringList)) {
      return Collections.emptyList();
    }
    Map<Integer, Type1SearchPackage> packageMap = new HashMap<>();
    Map<Integer, Type1SearchEntity> entityMap = new HashMap<>();
    LightZSKU lightZSKU = null;
    int type1;
    List<? extends VSKU> vskuList;
    Type1SearchEntity searchEntity;
    Map<String, Object> skuMap;
    for (String resultString : resultStringList) {
      try {
        skuMap = JsonOperator.getInstance().fromJson(resultString, Map.class);
      } catch (Exception e) {
        continue;
      }
      if(result_mode == 0){
        lightZSKU = LightZSKU.build(sp, skuMap, filters, regular, productCensus);
      }else {
      }
      if(lightZSKU == null){
        continue;
      }
      vskuList = lightZSKU.buildVSKUS();

      for(int i = 0; i< vskuList.size(); i++){
        VSKU vsku = vskuList.get(i);
        type1 = vsku.getType1();
        searchEntity = entityMap.get(type1);
        if(searchEntity == null){
          searchEntity = new Type1SearchEntity(type1);
          entityMap.put(type1, searchEntity);
        }
        searchEntity.addSku(vsku);
      }
    }

    List<Type1SearchEntity> list2 = new ArrayList<>(entityMap.size());
    for (Map.Entry<Integer, Type1SearchEntity> entry : entityMap.entrySet()) {
      list2.add(entry.getValue());
    }

    return list2;
  }

  public static List<Type1SearchEntity> packageByType1(SearchParameterEntity sp, List<String> resultStringList,
                                                       List<AbstractFilter> filters, AggregationRegular regular,
                                                       Set<Integer> census, int result_mode) {
    if (CollectionUtils.isEmpty(resultStringList)) {
      return Collections.emptyList();
    }
    Map<Integer, Type1SearchPackage> packageMap = new HashMap<>();
    Map<Integer, Type1SearchEntity> entityMap = new HashMap<>();
    LightLessZSKU lightZSKU = null;
    int type1;
    List<? extends VSKU> vskuList;
    Type1SearchEntity searchEntity;
    Map<String, Object> skuMap;
    for (String resultString : resultStringList) {
      try {
        skuMap = JsonOperator.getInstance().fromJson(resultString, Map.class);
      } catch (Exception e) {
        continue;
      }
      if(result_mode == 0){
        lightZSKU = LightLessZSKU.build(sp, skuMap, filters, regular, census);
      }else {
      }
      if(lightZSKU == null){
        continue;
      }
      vskuList = lightZSKU.buildVSKUS();

      for(int i = 0; i< vskuList.size(); i++){
        type1 = vskuList.get(i).getType1();
        searchEntity = entityMap.get(type1);
        if(searchEntity == null){
          searchEntity = new Type1SearchEntity(type1);
          entityMap.put(type1, searchEntity);
        }
        searchEntity.addSku(vskuList.get(i));
      }
    }

    List<Type1SearchEntity> list2 = new ArrayList<>(entityMap.size());
    for (Map.Entry<Integer, Type1SearchEntity> entry : entityMap.entrySet()) {
      list2.add(entry.getValue());
    }
    return list2;
  }


  public static SearchEntity packageAll(SearchParameterEntity sp, List<String> resultStringList,
                                        List<AbstractFilter> filters, AggregationRegular regular,
                                        AggregationProduct productCensus, int result_mode) {
    if (CollectionUtils.isEmpty(resultStringList)) {
      return null;
    }

    SearchEntity searchEntity = new SearchEntity();
    LightZSKU lightZSKU = null;
    int type1;
    List<? extends VSKU> vskuList;
    Map<String, Object> skuMap;
    //System.out.println("packageAll new sort 1 size:" + resultStringList.size());
    for (String resultString : resultStringList) {
      try {
        skuMap = JsonOperator.getInstance().fromJson(resultString, Map.class);
      } catch (Exception e) {
        continue;
      }
      if(result_mode == 0){
        lightZSKU = LightZSKU.build(sp, skuMap, filters, regular, productCensus);
      }else {

      }
      if (lightZSKU == null) {
        continue;
      }
      try {
        vskuList = lightZSKU.buildVSKUS();
        if(CollectionUtils.isNotEmpty(vskuList)) {
          searchEntity.addSku(vskuList.get(0));
        }
      }catch (Exception e){
        e.printStackTrace();
      }
    }

    //System.out.println("packageAll return size:" + searchEntity.getPackageSize());
    return searchEntity;
  }


  public static SearchEntity packageAll(SearchParameterEntity sp, List<String> resultStringList,
                                        List<AbstractFilter> filters, AggregationRegular regular,
                                        Set<Integer> setStg, int result_mode) {
    if (CollectionUtils.isEmpty(resultStringList)) {
      return null;
    }

    SearchEntity searchEntity = new SearchEntity();
    LightLessZSKU lightZSKU = null;
    int type1;
    List<? extends VSKU> vskuList;
    Map<String, Object> skuMap;
    //System.out.println("packageAll new sort 1 size:" + resultStringList.size());
    for (String resultString : resultStringList) {
      try {
        skuMap = JsonOperator.getInstance().fromJson(resultString, Map.class);
      } catch (Exception e) {
        continue;
      }
      if(result_mode == 0){
        lightZSKU = LightLessZSKU.build(sp, skuMap, filters, regular, setStg);
      }else {

      }
      if (lightZSKU == null) {
        continue;
      }
      try {
        vskuList = lightZSKU.buildVSKUS();
        if(CollectionUtils.isNotEmpty(vskuList)) {
          searchEntity.addSku(vskuList.get(0));
        }
      }catch (Exception e){
        e.printStackTrace();
      }
    }

    //System.out.println("packageAll return size:" + searchEntity.getPackageSize());
    return searchEntity;
  }

  ///////////////////////////
  public static List<Type1SearchEntity> packageBySTG(SearchParameterEntity sp, List<String> resultStringList,
                                                       List<AbstractIDFilter> filters, AggregationRegular regular,
                                                       AggregationProduct productCensus, int result_mode) {
    if (CollectionUtils.isEmpty(resultStringList)) {
      return Collections.emptyList();
    }
    Map<Integer, Type1SearchEntity> entityMap = new HashMap<>();
    SimpleZSKU lightZSKU = null;
    int type1;
    List<? extends VSKU> vskuList;
    Type1SearchEntity searchEntity;
    for (String resultString : resultStringList) {
      /*******************************************************/
      if("EMPTY".equals(resultString))
        System.out.println("EMPTY get~");
      /*******************************************************/
      if(result_mode == 0){
        lightZSKU = SimpleZSKU.build(sp, resultString, filters, regular, productCensus);
      }else {
      }
      if(lightZSKU == null){
        continue;
      }
      vskuList = lightZSKU.buildVSKUS();
      /*if(lightZSKU.getId() == 70258){
        System.out.println("PP:"+lightZSKU.getMarket_info());
      }*/
      for(int i = 0; i< vskuList.size(); i++){
        VSKU vsku = vskuList.get(i);
        type1 = vsku.getType1();
        searchEntity = entityMap.get(type1);
        if(searchEntity == null){
          searchEntity = new Type1SearchEntity(type1);
          entityMap.put(type1, searchEntity);
        }
        searchEntity.addSku(vsku);
      }
    }

    List<Type1SearchEntity> list2 = new ArrayList<>(entityMap.size());
    for (Map.Entry<Integer, Type1SearchEntity> entry : entityMap.entrySet()) {
      list2.add(entry.getValue());
    }

    return list2;
  }

  public static List<Type1SearchEntity> packageBySTG(SearchParameterEntity sp, List<String> resultStringList,
                                                       List<AbstractIDFilter> filters, AggregationRegular regular,
                                                       Set<Integer> census, int result_mode) {
    if (CollectionUtils.isEmpty(resultStringList)) {
      return Collections.emptyList();
    }
    Map<Integer, Type1SearchEntity> entityMap = new HashMap<>();
    SimpleZSKU lightZSKU = null;
    int type1;
    List<? extends VSKU> vskuList;
    Type1SearchEntity searchEntity;
    for (String resultString : resultStringList) {
      /*******************************************************/
      if("EMPTY".equals(resultString))
        System.out.println("EMPTY get~");
      /*******************************************************/
      if(result_mode == 0){
        lightZSKU = SimpleZSKU.build(sp, resultString, filters, regular, census);
      }else {
      }
      if(lightZSKU == null){
        continue;
      }
      vskuList = lightZSKU.buildVSKUS();

      for(int i = 0; i< vskuList.size(); i++){
        type1 = vskuList.get(i).getType1();
        searchEntity = entityMap.get(type1);
        if(searchEntity == null){
          searchEntity = new Type1SearchEntity(type1);
          entityMap.put(type1, searchEntity);
        }
        searchEntity.addSku(vskuList.get(i));
      }
    }

    List<Type1SearchEntity> list2 = new ArrayList<>(entityMap.size());
    for (Map.Entry<Integer, Type1SearchEntity> entry : entityMap.entrySet()) {
      list2.add(entry.getValue());
    }
    return list2;
  }


  public static SearchEntity packageAllSTG(SearchParameterEntity sp, List<String> resultStringList,
                                        List<AbstractIDFilter> filters, AggregationRegular regular,
                                        AggregationProduct productCensus, int result_mode) {
    if (CollectionUtils.isEmpty(resultStringList)) {
      return null;
    }
    SearchEntity searchEntity = new SearchEntity();
    SimpleZSKU lightZSKU = null;
    int type1;
    List<? extends VSKU> vskuList;
    //System.out.println("packageAll new sort 1 size:" + resultStringList.size());
    for (String resultString : resultStringList) {
      /*******************************************************/
      if("EMPTY".equals(resultString))
        System.out.println("EMPTY get~");
      /*******************************************************/
      if(result_mode == 0){
        lightZSKU = SimpleZSKU.build(sp, resultString, filters, regular, productCensus);
      }else {

      }
      if (lightZSKU == null) {
        continue;
      }
      try {
        vskuList = lightZSKU.buildVSKUS();
        if(CollectionUtils.isNotEmpty(vskuList)) {
          searchEntity.addSku(vskuList.get(0));
        }
      }catch (Exception e){
        e.printStackTrace();
      }
    }

    //System.out.println("packageAll return size:" + searchEntity.getPackageSize());
    return searchEntity;
  }


  public static SearchEntity packageAllSTG(SearchParameterEntity sp, List<String> resultStringList,
                                        List<AbstractIDFilter> filters, AggregationRegular regular,
                                        Set<Integer> setStg, int result_mode) {
    if (CollectionUtils.isEmpty(resultStringList)) {
      return null;
    }

    SearchEntity searchEntity = new SearchEntity();
    SimpleZSKU lightZSKU = null;
    int type1;
    List<? extends VSKU> vskuList;
    //System.out.println("packageAll new sort 1 size:" + resultStringList.size());
    for (String resultString : resultStringList) {
      /*******************************************************/
      if("EMPTY".equals(resultString))
        System.out.println("EMPTY get~");
      /*******************************************************/
      if(result_mode == 0){
        lightZSKU = SimpleZSKU.build(sp, resultString, filters, regular, setStg);
      }else {

      }
      if (lightZSKU == null) {
        continue;
      }
      try {
        vskuList = lightZSKU.buildVSKUS();
        if(CollectionUtils.isNotEmpty(vskuList)) {
          searchEntity.addSku(vskuList.get(0));
        }
      }catch (Exception e){
        e.printStackTrace();
      }
    }

    //System.out.println("packageAll return size:" + searchEntity.getPackageSize());
    return searchEntity;
  }

}